package org.smart4j.chapter.controller;

import org.smart4j.chapter.annotation.LtdProxy;
import org.smart4j.chapter.model.Customer;
import org.smart4j.chapter.service.CustomerService;
import org.smart4j.framework.annotation.Action;
import org.smart4j.framework.annotation.Aspect;
import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.bean.Data;
import org.smart4j.framework.bean.FileParam;
import org.smart4j.framework.bean.Param;
import org.smart4j.framework.bean.View;
import org.smart4j.framework.helper.ConfigHelper;
import org.smart4j.framework.helper.UploadHelper;
import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.framework.util.StringUtil;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chinalin on 2017/3/23.
 * 处理客户管理相关请求
 */
@LtdProxy
@Controller
public class CustomerController {

    @Inject
    private CustomerService customerService ;

     /**
     *进入 hello 界面
     */
    @Action("get:/hello")
    public View hello(){
        System.out.println("hello Controller 启动");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(new Date()) ;
        return  new View("hello.jsp").addModel("currentTime" , currentTime) ;
    }
//    /**
//     *进入 hello 界面
//     */
//    @Action("get:/b")
//    public View b(){
//        return  new View("b.jsp") ;
//    }

    /**
     *进入 客户列表 界面
     */
    @Action("get:/customer")
    public View index(){
        List<Customer> customerList = customerService.getCustomerList() ;
        return  new View("customer.jsp").addModel("customerList", customerList) ;
    }

//    /**
//     *显示客户基本信息
//     */
//    @Action("get:/customer_show")
//    public  View show(Param param){
//        long id = param.getLong("id");
//        Customer customer = customerService.getCustomer(id);
//        return  new View("customer_show.jsp").addModel("customer", customer) ;
//    }
//
    /**
     *进入 创建客户 界面
     */
    @Action("get:/customer_create")
    public  View create(){
        return  new View("customer_create.jsp");
    }

    @Action("post:/customer_create")
    public Data createSubmit(Param param){
        Map<String, Object> fieldMap = param.getFieldMap() ;
        /**
         * 将爱好这一项去掉，同时写入到备注这个字段去,数据库里没有爱好
         */
        if (CollectionUtil.isNotEmpty(fieldMap)){   //取得普通表单项
            for (Map.Entry<String,Object> fileMapEntry : fieldMap.entrySet()){
                String key = fileMapEntry.getKey() ;
                if (key.equalsIgnoreCase("hobby")){ //"爱好"写入备注去
                    Object value = fileMapEntry.getValue() ;
                    fieldMap.put("remark",value) ;
                    fieldMap.remove(key) ;
                }
            }
        }
        /**
         * 将上传的文件写入文件夹，同时将 文件字段名：文件路径 写入备注
         */
        Map<String, List<FileParam>> fileMap = param.getFileMap();
        if (CollectionUtil.isNotEmpty(fileMap)){        //取得上传文件表单
            String basePath = ConfigHelper.getAppUploadFileBasepath() ;
            for (Map.Entry<String,List<FileParam>> fileMapEntry : fileMap.entrySet()){
                String key = fileMapEntry.getKey() ;
                List<FileParam> fileParamList = fileMapEntry.getValue() ;
                if (CollectionUtil.isNotEmpty(fileParamList)){
                    //上传多个文件
                    UploadHelper.uploadFile(basePath,fileParamList);
                    if (CollectionUtil.isNotEmpty(fieldMap)) {   // customer的MAP不为空才能写入上传的文件路径
                        //上传的文件名写入 remark 字段
                        if (CollectionUtil.isNotEmpty(fileParamList)) {
                            for (FileParam fileParam : fileParamList) {
                                String fileName = fileParam.getFileName();
                                String remark = "";
                                if (fieldMap.containsKey("remark")){
                                    remark = (String) fieldMap.get("remark") + StringUtil.SEPARATOR;
                                }
                                remark =  remark + key + ":" + basePath + fileParam.getFileName() ;
                                fieldMap.put("remark", remark) ;
                            }
                        }
                    }
                }
            }
        }
        boolean result = customerService.createCustomer(fieldMap) ;
        return  new Data(result);
    }

//    /**
//     *进入 编辑客户 界面
//     */
//    @Action("get:/customer_edit")
//    public  View edit(Param param){
//        long id = param.getLong("id") ;
//        Customer customer = customerService.getCustomer(id) ;
//        return  new View("customer_edit.jsp").addModel("customer", customer);
//    }
//
//    /**
//     *处理 编辑客户 请求
//     */
//    @Action("put:/customer_edit")
//    public  Data editSubmit(Param param){
//        long id = param.getLong("id") ;
//        Map<String, Object> fielMap = param.getMap();
//        boolean result = customerService.updateCustomer(id,fielMap) ;
//        return  new Data(result);
//    }
//
//    /**
//     *处理 删除客户 请求
//     */
//    @Action("delete:/customer_edit")
//    public  Data delete(Param param){
//        long id = param.getLong("id") ;
//        boolean result = customerService.deleteCustomer(id) ;
//        return  new Data(result);
//    }

}

